package cn.edu.jxust.core.domain.repository.impl;

import cn.edu.jxust.common.domain.entity.RoleResource;
import cn.edu.jxust.common.domain.dto.RoleResourceVo;
import cn.edu.jxust.core.infrastructure.mapper.RoleResourceMapper;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.RoleResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author QiuKangming
 * @since 2020-06-29
 */
@Slf4j
@Service
public class RoleResourceServiceImpl extends ServiceImpl<RoleResourceMapper, RoleResource> implements RoleResourceService {

    @Resource
    private RoleResourceMapper roleResourceMapper;

    /**
     * 根据角色 id 获取资源 ids 集合
     * @param roleId 角色id
     * @return List<Integer>
     */
    @Override
    public List<Integer> getResourceIdsByRoleId(Integer roleId) {

        try{
            return roleResourceMapper.getResourceIdsByRoleId(roleId);
        }catch (Exception e){
            log.error("[RoleAuthorityServiceImpl --> getResourceIdsByRoleId] 报错: {}", e.getMessage());
            return null;
        }

    }

    /**
     * 更新角色权限
     * @param roleResourceVo 视图对象
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateResourceByRoleId(RoleResourceVo roleResourceVo) {

        try{
            Map<String, Object> map = new HashMap<>();
            map.put("role_id", roleResourceVo.getRoleId());
            roleResourceMapper.deleteByMap(map);
            List<Integer> permissionIdsList;
            String authcIds = roleResourceVo.getResourceIds();
            if(!StringUtils.isEmpty(authcIds)){
                String[] permissionIds = authcIds.split(",");
                if(permissionIds.length > 0){
                    permissionIdsList = new ArrayList<>();
                    for (String permissionId : permissionIds) {
                        permissionIdsList.add(Integer.parseInt(permissionId));
                    }
                }else{
                    //走到这 说明撤销全部权限, 不用再写数据库
                    return ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "成功撤销该角色的所有权限");
                }
            }else{
                //走到这 说明撤销全部权限, 不用再写数据库
                return ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "成功撤销该角色的所有权限");
            }
            List<RoleResource> roleResourceList = new ArrayList<>();

            for (Integer permission : permissionIdsList) {
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleId(roleResourceVo.getRoleId());
                roleResource.setId(null);
                roleResource.setResourceId(permission);
                roleResource.setEditor(roleResourceVo.getEditor());
                roleResourceList.add(roleResource);
            }
            return this.saveBatch(roleResourceList, roleResourceList.size())
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "成功为该角色分配权限")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "为该角色分配权限失败");
        }catch(Exception e){
            log.error("RoleAuthorityServiceImpl --> updateAuthcByRoleId: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }
}
